<?php
/**
 * Description of ArtistiController
 *
 * @author luigi.pastore
 * @created  4-mag-2011
 */
class ArtistiController extends Classes_Controller
{
    public function init()
    {
	$this->model_artisti	= new Classes_Artist();
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'artisti/index' );
    }
    
    public function nuovoAction()
    {
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );

	    if( !$errorMsg AND $_FILES['artist_image']['name'] != '' )
	    {
		$this->uploader = new Classes_Upload( $_FILES['artist_image'] );
		
		$errorMsg = $this->handleImage( null, false );		    
	    }

	    if( !$errorMsg )
	    {
		// Mapping...
		$data = array(
		    'artist_name' => $_POST['artist_name'],
		    'artist_info' => $_POST['artist_info'],
		    'artist_official' => $_POST['artist_official'],
		    'artist_facebook' => $_POST['artist_facebook'],
		    'artist_myspace' => $_POST['artist_myspace'],
		    'artist_createdby' => $this->application->auth->user['user_id']
		);
				
		// Se è stata caricata un'immagine aggiorna, altrimenti no...
		if( $this->uploader->file_dst_name_ext )
		    $data['artist_image_ext'] = $this->uploader->file_dst_name_ext;
		    
		$model = new Classes_Artists( $data );

		if( is_numeric( $artist_id = $model->save()->artist_id ) )
		{
		    if( $this->uploader )
		    {
			$errorMsg = $this->handleImage( $artist_id, true );
		    }
			
		    if( !$errorMsg )
		    {
			// Rimanda alla pagina di conferma e cancella file temporanei...
			$this->layout->forward = 'artisti/archivio';
			
			return $this->layout->content = array( 'script' => 'completed' );
		    }
		}
		else
		{
		    trigger_error( 'Errore Inserimento Artista' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'artisti/nuovo' );
    }

    public function editAction()
    {
	// Edit
	if( !is_null( $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $artist = $this->model_artisti->fetchById( array( $this->getParam( 'id' ) ) );

	    // Se non c'è il post lancia l'errore
	    if( !is_array( $artist ) )
	    {
		trigger_error( 'Errore Edit Artista', E_USER_WARNING );
	    }
	    else
	    {
		$artist = array_shift( $artist );
		
		# Assegniamo i dati del post al layout
		$this->layout->data = $artist;
	    }
	}
	
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );

	    if( !$errorMsg AND $_FILES['artist_image']['name'] != '' )
	    {
		$this->uploader = new Classes_Upload( $_FILES['artist_image'] );
		
		$errorMsg = $this->handleImage( null, false );
	    }

	    if( !$errorMsg )
	    {
		// Mapping...
		$data = array(
		    'artist_id' => $this->getParam( 'artist_id' ),
		    'artist_name' => $_POST['artist_name'],
		    'artist_info' => $_POST['artist_info'],
		    'artist_official' => $_POST['artist_official'],
		    'artist_facebook' => $_POST['artist_facebook'],
		    'artist_myspace' => $_POST['artist_myspace'],
		    'artist_modifiedby' => $this->application->auth->user['user_id']
		);
				
		// Se è stata caricata un'immagine aggiorna, altrimenti no...
		if( $this->uploader )
		    $data['artist_image_ext'] = $this->uploader->file_dst_name_ext;
		else
		    $data['artist_image_ext'] = $artist['artist_image_ext'];
		    
		$model = new Classes_Artists( $data );

		if( is_numeric( $artist_id = $model->save()->artist_id ) )
		{
		    if( $this->uploader )
		    {
			$errorMsg = $this->handleImage( $artist_id, true );
		    }
			
		    if( !$errorMsg )
		    {
			// Rimanda alla pagina di conferma e cancella file temporanei...
			$this->layout->forward = 'artisti/archivio';
			
			return $this->layout->content = array( 'script' => 'completed' );
		    }
		}
		else
		{
		    trigger_error( 'Errore Inserimento Artista' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'artisti/edit' );	
    }
    
    public function archivioAction()
    {
	$this->layout->artists = $this->model_artisti->fetchAll();
	$this->layout->content = array( 'script' => 'artisti/archivio' );
    }
    
    private function checkFormData( $data )
    {
	if( !filter_var( $data['artist_name'], FILTER_SANITIZE_STRING ) )
	    $errorMsg['artist_name'] = sprintf( ERR_FORMAT, 'Nome Artista' );

	if( !filter_var( $data['artist_info'], FILTER_SANITIZE_SPECIAL_CHARS ) )
	    $errorMsg['artist_info'] = ERR_EMPTY;

	if( $_POST['artist_official'] != '' AND !filter_var( $data['artist_official'], FILTER_VALIDATE_URL ) )
	    $errorMsg['artist_official'] = sprintf( ERR_FORMAT, 'Sito ufficiale' );
	if( $_POST['artist_facebook'] != '' AND !filter_var( $data['artist_facebook'], FILTER_VALIDATE_URL ) )
	    $errorMsg['artist_facebook'] = sprintf( ERR_FORMAT, 'Facebook' );
	if( $_POST['artist_myspace'] != '' AND !filter_var( $data['artist_myspace'], FILTER_VALIDATE_URL ) )
	    $errorMsg['artist_myspace'] = sprintf( ERR_FORMAT, 'Myspace' );
	
	return $errorMsg ? $errorMsg : false;
    }
    
    private function handleImage( $artist_id, $process = false )
    {
	if( $this->uploader->uploaded )
	{
	    // L'immagine è stata caricata correttamente...
	    $this->uploader->allowed = array( 'image/*' );
	    $this->uploader->image_min_width = 100;
	    $this->uploader->file_safe_name = false;
	    $this->uploader->file_auto_rename = false;
	    $this->uploader->file_overwrite = true;
	    $this->uploader->jpeg_quality = 72;

	    if( $this->uploader->image_src_x > 190 )
	    {
		$this->uploader->image_resize = true;
		$this->uploader->image_x = 190;
		$this->uploader->image_ratio_y = true;
	    }

	    // Processa
	    if( $process === true )
	    {
		// Rinomina e carica...
		$this->uploader->file_new_name_body = 'artist_' . $artist_id;
		$this->uploader->process( UPLOAD_DIR );

		if( $this->uploader AND !$this->uploader->processed )
		{
		    // Aggiungi l'errore
		    $errorMsg['artist_image'] = $this->uploader->error;
		}
			
		$this->uploader->clean();
	    }
	    else
	    {
		$this->uploader->process();
	    }

	    // Tutto ok?
	    if( !$this->uploader->processed )
	    {
		# Aggiungi l'errore
		$errorMsg['artist_image'] = $this->uploader->error;
	    }
	}
	else
	{
	    // Aggiungi l'errore
	    $errorMsg['artist_image'] = $this->uploader->error;	    
	}
	
	return $errorMsg ? $errorMsg : false;
    }
}